package com.sebiotec.ambassador.adapt;

import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;

import com.allen.library.base.BaseHttpUtil;
import com.allen.library.interceptor.Transformer;
import com.allen.library.observer.CommonObserver;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sebiotec.ambassador.R;
import com.sebiotec.ambassador.model.post.PostJobDetail;
import com.sebiotec.ambassador.model.response.JobListResponse;

import com.sebiotec.ambassador.net.BookApiHelper;
import com.sebiotec.ambassador.ui.activity.JobDetailActivity;
import com.sebiotec.ambassador.ui.activity.WecomeActivity;
import com.sebiotec.ambassador.ui.view.LoadingDialog;
import com.sebiotec.basecommon.utils.constant.SjmConstant;
import com.sebiotec.basecommon.utils.language.LocalManageUtil;
import com.sebiotec.basecommon.utils.tools.ActivityTool;
import com.sebiotec.basecommon.utils.tools.SPUtils;
import com.jakewharton.rxbinding2.view.RxView;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.GravityEnum;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;
import okhttp3.ResponseBody;

public class JobAdapt extends BaseQuickAdapter<JobListResponse.RowsBean, BaseViewHolder> {

    public JobAdapt(int layoutResId, @Nullable List<JobListResponse.RowsBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, JobListResponse.RowsBean item) {
        helper.setText(R.id.tv_upcoming_name, item.getName());
        helper.setText(R.id.tv_num, item.getPassengers() + "");
        CardView cardView= helper.itemView.findViewById(R.id.card_job);
        if (item.getReadStatus()==0){
            //未读
            helper.itemView.findViewById(R.id.v_xhd).setVisibility(View.VISIBLE);
        }else{
            helper.itemView.findViewById(R.id.v_xhd).setVisibility(View.GONE);

        }


        if ("".equals(item.getServesUser())){

            helper.itemView.findViewById(R.id.ll_dx).setVisibility(View.GONE);
        }
        switch (item.getStatus()) {
            //1:Ambassador pickup 2:Driver pickup 3: Completed(Driver pickdown) 4:Rejected
            case 1:
                helper.setText(R.id.tv_titles,"Confirmed by"+" "+item.getServesUser());
                break;
            case 2:
                helper.setText(R.id.tv_titles, "Picked-up by"+" "+item.getServesUser());
                break;
            case 3:
                helper.setText(R.id.tv_titles, "Picked-up by"+" "+item.getServesUser());
                break;
            case 4:
                helper.setText(R.id.tv_titles, "Droped-off by"+" "+item.getServesUser());
                break;
            case 5:
                helper.setText(R.id.tv_titles, "Completed by"+" "+item.getServesUser());
                break;
            case 6:
                helper.setText(R.id.tv_titles, "Assigned vehicle by"+" "+item.getServesUser());
                break;
            case 7:
                helper.setText(R.id.tv_titles, "Assigned driver by"+" "+item.getServesUser());
                break;
            case 8:
                helper.setText(R.id.tv_titles, "Assigned ambassador by"+" "+item.getServesUser());
                break;
            case -2:
                helper.setText(R.id.tv_titles, "Cancelled by"+" "+item.getServesUser());
                break;
            case -1:
                helper.setText(R.id.tv_titles, "Rejected by"+" "+item.getServesUser());
                break;

            default:
                helper.setText(R.id.tv_titles, "Ambassador by"+" "+item.getServesUser());
                break;
        }
        helper.setText(R.id.tv_address, item.getPickupFrom());
        helper.setText(R.id.tv_pickup_time,item.getPickupDateTime());
        helper.setText(R.id.tv_to_address, item.getDestination());
        helper.setText(R.id.tv_recevied_by,item.getServesDateTime());




        RxView.clicks( cardView)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        helper.itemView.findViewById(R.id.v_xhd).setVisibility(View.GONE);

                        Intent intent =new Intent(getContext(), JobDetailActivity.class);
                        intent.putExtra("pos",helper.getLayoutPosition());
                        intent.putExtra("status",item.getStatus());
                        intent.putExtra("readStatus",item.getReadStatus());
                        intent.putExtra("BookId",item.getBookingId());
                        getContext().startActivity(intent);
                    }
                });


        RxView.clicks(helper.itemView.findViewById(R.id.btn_sigin))
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        Intent intent =new Intent(getContext(), WecomeActivity.class);

                        intent.putExtra("BookId",item.getBookingId());
                        getContext().startActivity(intent);
                    }
                });


        RxView.clicks( helper.itemView.findViewById(R.id.btn_compete))
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        new MaterialDialog.Builder(getContext())
                                .content(getContext().getString(R.string.job_compete))
                                .contentGravity(GravityEnum.CENTER)
                                .negativeColor(getContext().getResources().getColor(R.color.smj))
                                .positiveColor(getContext().getResources().getColor(R.color.smj))
                                .buttonsGravity(GravityEnum.CENTER)
                                .positiveText(getContext().getString(R.string.yes))
                                .negativeText(getContext().getString(R.string.no_login))
                                .onPositive(new MaterialDialog.SingleButtonCallback() {
                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                            ActivityTool.getInstance().finishAllActivities();
//                          ARouter.getInstance().build("/Login/LoginActivity").navigation(getContext().getApplicationContext());


                                            LoadingDialog loadingDialog = new LoadingDialog(ActivityTool.getInstance().getLastActivity());
                                            HashMap<String,Object> map=new HashMap<String, Object>();
                                            map.put("LoginId", SPUtils.get(SjmConstant.LOGINID,""));
                                            map.put("Token", SPUtils.get(SjmConstant.TOKEN,""));
                                            map.put("Lang", LocalManageUtil.getSMJSetLanguageLocales(ActivityTool.getInstance().getLastActivity()));
                                            PostJobDetail bean=new PostJobDetail(item.getBookingId(),2);
                                            BookApiHelper.geRadomApi()
                                                    .UpdateJob(map,item.getBookingId(),new BaseHttpUtil<PostJobDetail>().convertVo2Json(bean))
                                                    .compose(Transformer.<ResponseBody>switchSchedulers(loadingDialog))
                                                    .subscribe(new CommonObserver<ResponseBody>() {
                                                        @Override
                                                        protected void onError(String errorMsg) {
                                                        }

                                                        @Override
                                                        protected void onSuccess(ResponseBody s) {

                                                            remove(helper.getLayoutPosition());


                                                        }
                                                    });

                                        }





                                }).show();
                    }
                });

//        TextView viewById = helper.itemView.findViewById(R.id.tv_upcoming_name);
//        TextView viewById1 = helper.itemView.findViewById(R.id.tv_titles);
//        TextView viewById2 = helper.itemView.findViewById(R.id.tv_recevied_by);
//        viewById.setTextColor(getContext().getColor(R.color.black));
//        viewById1.setTextColor(getContext().getColor(R.color.black));
//        viewById2.setTextColor(getContext().getColor(R.color.black));


    }
}